import os
import sys
import requests
import json
from config import *
from stacked_area import *
from bar_percentage import *
from stacked_bar import *
from profitCompChart import *
from line_base import *


def get_json_from_url(url):
    response = requests.get(url)
    if response.status_code == 200:
        json_data = response.json()
        return(json_data)
    else:
        print("Failed to get JSON data from URL")

def convert_stocknum(stocknum):
    if (stocknum[0] == '6'):
        return stocknum + ".SH"
    elif (stocknum[0] == '0'):
        return stocknum + ".SZ"
    else:
        return ""

def makechart(STOCKCODE,FromYear,ToYear,ReportTime):
    #综合财务数据报表制作。
    # url_main = make_url_mian(STOCKCODE)
    # data_main = get_json_from_url(url_main)

    # print ("***chart main made done!!")


    url_zichan = make_url_zichan(STOCKCODE,FromYear,ToYear,ReportTime)
    print ("爬虫获取资产负债表数据连接如下:\n",url_zichan)
    data_zichan = get_json_from_url(url_zichan)
    # json_data = json.dumps(data_zichan)
    # with open("zichan_data.json","w") as fd:
    #     fd.write(json_data)

    url_lirun = make_url_lirun(STOCKCODE,FromYear,ToYear,ReportTime)
    print ("爬虫获取利润表连接如下:\n",url_lirun)
    data_lirun = get_json_from_url(url_lirun)
    # json_data = json.dumps(data_lirun)
    # with open("lirun_data.json","w") as fd:
    #     fd.write(json_data)

    url_cash = make_url_cash(STOCKCODE,FromYear,ToYear,ReportTime)
    print ("爬虫获取现金流量表连接如下:\n",url_cash)
    data_cash = get_json_from_url(url_cash)
    # json_data = json.dumps(data_cash)
    # with open("cash_data.json","w") as fd:
    #     fd.write(json_data)



    #资产堆积图制作
    makechart_zichan(data_zichan)
    print ("\n\n*****资产堆积图制作完成!!*******")

    #负债堆积图制作
    makechart_fuzhai(data_zichan)
    print ("*****负债堆积图制作完成!!*******")

    #利润表百分比图制作
    makechart_lirun(data_lirun)
    print ("*****利润表分析图制作完成!!*******")

    #现金流量表制作
    makechart_cash(data_cash)
    print ("*****现金流量分析图制作完成!!*******")

    #净利润现金流对比分析图制作
    makechart_liruncash(data_lirun,data_cash)
    print ("*****净利润现金流对比分析图制作完成!!*******")


    print ("***************done!!***************\n")

def makeCompChart(codelist,Ctype="TOTAL_PROFIT", FromYear=2023, ToYear=2014, ReportTime=4):
    datadict = {}
    xdatalist = []
    for code in codelist:
        stocknum = convert_stocknum(code)
        url_zichan = make_url_zichan(stocknum,FromYear,ToYear,ReportTime)
        url_lirun = make_url_lirun(stocknum,FromYear,ToYear,ReportTime)
        url_cash = make_url_cash(stocknum,FromYear,ToYear,ReportTime)
        data_zichan = get_json_from_url(url_zichan)
        data_lirun = get_json_from_url(url_lirun)
        data_cash = get_json_from_url(url_cash)
        stockName = data_zichan["result"]["data"][0]["SECURITY_NAME_ABBR"]
        xdatalist.append(stockName)
        data = {}
        data["Name"] = stockName
        data["zichan"] = data_zichan
        data["lirun"] = data_lirun
        data["cash"] = data_cash
        datadict[code] = data

    makechart_compare(datadict,xdatalist,FromYear,ToYear,Ctype)


helpstr = '''
**************Usage:
--help :打印命令说明
--base [代码]: 打印目标代码的三张财报报表
--compp [代码1] [代码2] ...：生成多个代码的利润比较
'''

if __name__ == '__main__':
    args = sys.argv
    if len(args) < 3:
        print ("**********parameters is not enough!!********* ")
        print (helpstr)
    else:
        funtype = args[1]
        if (funtype == "--help"):
            prnit (helpstr)
        elif (funtype == "--base"):
            stocknum = convert_stocknum(args[2])
            if (len(stocknum) == 0):
                print ("Not a good StockNum!!")
                exit(-1)
            #make chart!
            makechart(STOCKCODE=stocknum, FromYear=2023, ToYear=2014, ReportTime=4)
        elif (funtype == "--compp"):
            codelist = args[2:]
            makeCompChart(codelist)
        else:
            print ("*********No This Funtion!!**********")